﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class TempStudentService
    {
       readonly ITempStudentEntity entity;

       public TempStudentService()
       {
           this.entity = new TempStudentEntity();

       }

       #region Sqlcommand
         string GetMaxCode = "select top 1 Code from TempStudent  order by ID desc ";
       #endregion

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model, ref string mess)
       {
           try
           {
               if (Validate((TempStudent)_model, ref mess))
               {
                   var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                   if (id == 0)
                   {
                       return entity.Save((CoreData.TempStudent)_model, Table.TempStudent.ToString());
                   }
                   else
                   {
                       return entity.Update((CoreData.TempStudent)_model, Table.TempStudent.ToString());
                   }
               }
               else
               {
                   return -1;
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.TempStudent GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.TempStudent.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.TempStudent> GetList()
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllTempStudent," TempStudent.IsDeleted ='false'")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.TempStudent> GetListByLINQ(Func<CoreData.TempStudent, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.TempStudent.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.TempStudent GetOneByLINQ(Func<CoreData.TempStudent, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.TempStudent.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.TempStudent> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllTempStudent, " TempStudent.IsDeleted ='false' and "+_searchstring +" Order by ContactDate DESC")).ToList();
           }
           catch { return null; }
       }

       

       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.TempStudent)_model, Table.TempStudent.ToString());
               return true;
           }
           catch { return false; }
       }

       /// <summary>
       /// Delete a object by hidden
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool UpdateIsDelete(object _model)
       {
           try
           {
               var obj = (CoreData.TempStudent)_model;
               obj.IsDeleted = true;
               entity.Update(obj, Table.TempStudent.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method


       public IList<CoreData.TempStudent> GetByStatusID(long Status)
       {
           return GetList(" TempStudent.Status=" + Status);
       }


       public IList<CoreData.TempStudent> GetByChannelID(long Channel)
       {
           return GetList(" TempStudent.ChannelInfoID =" + Channel);
       }

       bool Validate(TempStudent obj, ref string mess)
       {

           new ValidateRequired("Mã", "Code", obj.Code);
           new ValidateRequired("Tên", "Name", obj.Name);
           new ValidateRequired("Họ", "FirstName", obj.FirstName);
           new ValidateRequired("Tên", "LastName", obj.LastName);
           if (obj.Status == 0)
           {
               new ValidateRequired("Trạng thái", "Status", "");
           }
          
           if (obj.ChannelInfoID == 0)
           {
               new ValidateRequired("Kênh thông tin", "Channel", "");
           }
           var objects = GetList().Cast<object>().ToList();
           new ValidateExist("Mã", "Code", "Code", obj, objects);
           new ValidateExist("Email", "Email", "Email", obj, objects);
           new ValidateEmail("Email", "Email", obj.Email);
           new ValidateLenght("Email", "Email", obj.Email, 50);
           new ValidateLenght("Điện thoại", "Phone", obj.Phone, 250);
           mess = BusinessObject.Validate();
           BusinessObject.Clear();
           return mess == null ? true : false;
       }

       public string GenerateCode()
       {
          
           string Code = "";
           string AutoNum = "001";
           string day = DateTime.Now.Day.ToString();
           string month = DateTime.Now.Month.ToString();
           string year = DateTime.Now.Year.ToString().Substring(DateTime.Now.Year.ToString().Length - 2);
           var data = entity.GetByCusTomSQL(GetMaxCode).FirstOrDefault();
           if (day.Length < 2)
           {
               day = day.Insert(0, "0");
           }
           if (month.Length < 2)
           {
               month = month.Insert(0, "0");
           }

           if (data == null)
           {
               Code = "HVT" + day + month + year + "001";
           }
           else
           {

               AutoNum = data.Code.Substring(9);
               int autonum = int.Parse(AutoNum) + 1;
               if (autonum <= 9)
               {
                   AutoNum = "00" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   AutoNum =  "0" + autonum.ToString();
               }
               else
               {
                   AutoNum = autonum.ToString();
                }
               Code = "HVT" + day + month + year  +AutoNum;
           }
           
           return Code;
       }

        #endregion 
    
    }
         
}
